<?

/*	Media.album OBJECT
*
*	Info:
*	This is a big one. Media.album is a collection of album elements and its navigation.
*	All in a shiny black rounded style.
*
*	The Structure:
*
*	CLUSTER			an overview of multiple albums
*	ALBUM			an animated album element, displaying a loop of multiple thumbnails
*	
*	NAV.Target		the "big" image
*	NAV.Bigslide	a moviereel-like sequence of thumbs
*	NAV.Slide		a Flickr-like nav, animated slide of thumb, numbered
*	NAV.Thumbs		a static thumbnails overview
*
*	A Media.album module is a collection of one or more of the above elements.
*	Multiple modules can be connected with an identifier.
*
*
*	[ Media.album is a good "extended" example ]

*	Methods:
*	output	-> html string
*	ajax	-> javascript string
*
*	Constructor:
*	
*					*/




class MediaAlbum extends MOD {

#	function ajax()		-> from MOD class

#	Construct creates object before html cycle. 
	public function construct() {
		global $Album, $Object, $Page;
	
		if( !$id = (int) $_GET['id'] ) return false;
		
		if( !$Object ) {
			$Album = new OBJECT(( array ) $this->vars, $id );
			$Page->set_cache( "title", $Album->get( 'name' ));
		}
	}

#	HTML loop module request	
	public function output() {
		global $Album, $Object;
				
		if( !$Album && !$Object && $this->vars->id ) $Album = new OBJECT(( array ) $this->vars, $this->vars->id ); 
		else if( !$Album && $Object ) {
			$Album = $Object;
			$Album->_addvars( $this->vars );	
		}
		
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();
		
		
		if( $nodes ) {
			$this->addclass( $this->action );
			$result = implode( "\n", $nodes );
		}
		
		return $this->parse( $result ) . $this->render_js();
		
	}

	
#	HTML Module.
#	Output actions	

#	Action cluster
	private function filter_cluster() {
		global $DB;

		if( $cluster = $DB->filter_key( $this->vars->album->type, "active order by llog desc", "ID" ))
			
			foreach( $cluster as $album ) $albums[] = $this->render_album( $album[ 'ID' ]);
		
		$albums[] = "<div class='stable'></div>";
	
		return $albums;
	}
	
#	Action raw images
	private function filter_raw() {
		global $DB, $Album;
		
		if( $Album->media )
			foreach( $Album->media->reference( "media.img" ) as $id )
				$nodes[] = $this->render_thumb( new OBJECT( $this->vars->objtype, (int) $id ), false, true );
		
		# If $nav, do the js thing.
		//$this->js = "mod = new MediaAlbum( '$nav', ". $Album->json( "ID", "album", "name" ) ." )";
		
		return $nodes;
	}

#	Action album
	private function filter_album() {
		global $DB;
		
		if( $this->vars->id )
			return $this->render_album( $this->vars->id );
	}
	
#	Action target (img container)
	private function filter_target() {
		global $DB, $Album, $Object;
		
		if( $this->vars->id )
			$Album = new OBJECT( ( array ) $this->vars, (int) $this->vars->id );
			
		else if( $this->vars->childobj ) {
			$Album = $Object->{ $this->vars->childobj };
			$Album->_addvars( $this->vars );
		}
			
		if( $this->vars->objtle )
			$this->name = $Object->get( 'name' );
		
		$this->set_attribute( "id", $this->vars->anchor );
		
		if( $Album || $this->vars->objects ) {
			$nodes[] = "<img class='cont' />";
			
			if( !$Object && $Album && !$this->vars->notitle ) $this->set_title( $Album->get( "name" ), "first" );

			if( $nav = $this->vars->navtype )
				$nodes[] = $this->{ "render_" . $nav }( $nav );
		
		} else {
		
			$nodes[] = ( $this->vars->imgid )?
				"<img class='cont' src='/public.img/sze={$this->vars->size}&id={$this->vars->imgid}' alt=\"". engine_get( "title" ) ."\" />" : "Empty.";
		}
		
		return $nodes;
	}


	
#	Action nav slide
	private function filter_nav() {
		global $DB, $Album;
		
		$this->addclass( $this->vars->navtype );
		
		if( $nav = $this->vars->navtype )
			$nodes = $this->{ "render_" . $nav }( $nav );
				
		return $nodes;
	}
	

#	RENDER.
#	Render (create html) albums, navs and content
	
#	Render album
	private function render_album( $id ) {
		global $DB;

		$Album = new OBJECT(( array ) $this->vars->album, $id );
		
		if( $imgcount = count( $imgs = $Album->media->reference( $Album->objtype ))) {
			//echo 'count: ' . $imgcount. ' - id:'. $id . ' - limit: ' . $Album->limit . ' - rand:' . (( count( $imgs ) < $Album->limit )? count( array_flip( $imgs )) : $Album->limit ). '<br />';
			/*if( $id == 190 )
				echo count( array_flip( $imgs ));*/
			$Album->set_cache( "media", array_rand( array_flip( $imgs ), ( count( array_flip( $imgs )) < $Album->limit )? count( array_flip( $imgs )) : $Album->limit ));
			$Album->set_cache( "album", $this->vars->album );
		
			$album = "<span class='album' ref='$id'><div class='thumb'></div><span class='name'>". $Album->get( 'name' ) . "</span>";
			$album.= "<span class='descr'>(" . count( array_flip( $imgs )) . " image" .(((int) count( array_flip( $imgs )) > 1)? "s":""). ")</span></span>";
		
			$this->js .= "\nmod = new MediaAlbum( 'album', ". $Album->json( "name", "album", "media" ) .", $id );";
		}
		
		return $album;
	}






#	Render NAV.hover
	private function render_hover( $class ) {
		global $DB, $Album;
		
				
		if( $Album && !$this->vars->override_album ) {
			$Album->set_cache( "media", $group = $Album->media->reference( $Album->objtype ));
			$Album->set_cache( "album", $this->vars );
			$this->js = "mod = new MediaAlbum( 'target', ". $Album->json( "ID", "album", "media" ) ." );";
		
		} else if( $this->vars->objects ) {
			if( $objects = $DB->filter_objects( $this->vars->type ))
				foreach( $objects as $Obj )
					if( $id = $Obj->media->get( $this->vars->objtype )->ID )
						$group[] = array( "name"=>$Obj->get( "name" ), "id"=> $id, "uri"=> $Obj->uri());
			
			$this->js = "mod = new MediaAlbum( 'target', " . json_encode( array( "album"=> $this->vars, "media"=> $group )) ." );";
			$link = "<span class='link'></span>";
		}
		
		if( count( $group ) > 1 )
			$nav = "<span class='nav $class linked'><span class='left'></span>$link<span class='right'></span><span class='info'></span></span>";
		
		return $nav;
	}
	
#	Render NAV.classic
	private function render_classic( $class ) {
		global $DB, $Album;
		
		if( !$group = $Album->media->reference( $Album->objtype )) $group = array();
		
		if( $this->vars->collect )
			foreach( $this->vars->collect as $part )
				if( $add = $Album->{ $part }->media->reference( $Album->objtype ))
					$group = array_merge( $group, $add );		
		
		
		$Album->set_cache( "media", $group );
		$Album->set_cache( "album", $this->vars );
		
		$this->js = "mod = new MediaAlbum( 'target', ". $Album->json( "ID", "album", "media" ) ." );";
		
		if( count( $group ) > 1 )
			$nav = "<span class='nav $class'><span class='left'></span><span class='right'></span><span class='info'></span></span>";
		
		return $nav;
	}

#	Render NAV.slide
	private function render_slide( $nav ) {
		global $DB, $Album;
		
		if( !$Album ) return false;
		
		$Album->set_cache( "album", $this->vars );
		
		$nodes[] = "<span class='reel'><span class='in'></span></span>";
		$nodes[] = "<span class='left'></span><span class='info'></span><span class='right'></span>";
		
		$this->js = "mod = new MediaAlbum( '$nav', ". $Album->json( "ID", "album", "name" ) ." );";
		
		return $nodes;
	}
	
#	Render NAV.snake
	private function render_snake( $nav ) {
		global $DB, $Album;
		
		$Album->set_cache( "album", $this->vars );
		
		$nodes[] = "<span class='left'></span><span class='reel'><span class='in'></span></span>";
		$nodes[] = "<span class='right'></span>";
		
		$this->js = "mod = new MediaAlbum( '$nav', ". $Album->json( "ID", "album", "name" ) ." );";
		
		return $nodes;
	}

#	Render NAV.thumbs
	private function render_thumbs( $nav ) {
		global $DB, $Album;
		
		$Album->set_cache( "album", $this->vars );
		
		$nodes[] = "<span class='nav'></span>";
		
		$this->js = "mod = new MediaAlbum( '$nav', ". $Album->json( "ID", "album", "name" ) ." );";
		
		return $nodes;
	}
	
#	Render thumb (basic)
	private function render_thumb( $el, $url = false, $raw = false ) {
		global $Album, $DB;

		$descr = ( $el->get( 'nameid' ))? $el->get( 'name' ): $el->get( 'raw' );
			
		$url = ( $url )? "<a href='$url{$el->id}'>" :(( $raw )? "<a href='/DB/media/{$el->id}.jpg' target='_blank'>": "" );
		
		return "<span class='img'>$url<img src='/public.img?sze={$this->vars->imgsize}&id={$el->id}' />$descr" . (( $url )? "</a>": "" ) . "</span>";
	}

}





/*
Media cluster:

{ "mod":"media.album", "action":"cluster", "vars":{
	
	"album"		: {
		"type"		: "group.img",
		"objtype"	: "media.img",
		"size"		: "124.142",
		"link"		: "/beeld/album",
		"limit"		: 6,
		"fade"		: 0.8,
		"interval"	: 6200
	}},
	"nameid"	: 2925
}


----------------
Media nav target:

{ "mod":"media.album", "action":"target", "vars":{
	"type"		: "group.img",
	"objtype"	: "media.img",
	"navtype"	: "hover",
	"anchor"	: "imgtarget",
	"size"		: "450.0",
	"fade"		: 0.28,
	"interval"	: 8000
}}

{ "mod":"media.album", "action":"target", "vars":{
	"type"		: "event.out",
	"objtype"	: "media.img",
	"navtype"	: "hover",
	"anchor"	: "imgtarget",
	"size"		: "300.0",
	"fade"		: 0.28,
	"interval"	: 12000
	}, "style"	: "margin-bottom:0px;"
}

----------------
Media nav slide:

{ "mod":"media.album", "action":"nav", "vars":{
	"anchor"	: "imgtarget",
	"type"		: "group.img",
	"objtype"	: "media.img",
	"navtype"	: "slide",
	"size"		: "72.60",
	"fade"		: 0.4,
	"interval"	: 8000,
	"extend_thumbs" : 9,
	"thumbsize"	: "138.28"
}}

----------------
Media nav snake:

{ "mod":"media.album", "action":"nav", "vars":{
	"anchor"	: "imgtarget",
	"type"		: "group.img",
	"objtype"	: "media.img",
	"navtype"	: "snake",
	"size"		: "48.38"
}}


*/


?>